//数组转树

var list = [
    {id: 1, name: "child1", parentId: 0},
    {id: 2, name: "child2", parentId: 0},
    {id: 6, name: "child2_1", parentId: 2},
    {id: 0, name: "root", parentId: null},
    {id: 5, name: "child1_2", parentId: 1},
    {id: 4, name: "child1_1", parentId: 1},
    {id: 3, name: "child3", parentId: 0},
    {id: 7, name: "child3_1", parentId: 3}
];
var map = {};
list.forEach(node => {
    map[node.id] = node;
    node.children = [];
});
var root;
list.forEach(node => {
    if (node.parentId === null) {
        root = node;
    } else {
        if (!map[node.parentId].children) {
            map[node.parentId].children = [];
        }
        map[node.parentId].children.push(node);
    }
});
console.log(root);